/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * Subject2descDAO.java
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.daosupport;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sundy.service.Subject2desc;

/**
 * A data access object (DAO) providing persistence and search support for
 * Subject2desc entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.sundy.service.Subject2desc
 * @author MyEclipse Persistence Tools
 */

public class Subject2descDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(Subject2descDAO.class);
	// property constants
	public static final String SUBJECT_ID = "subjectId";
	public static final String DESC_ID = "descId";

	protected void initDao() {
		// do nothing
	}

	public void save(Subject2desc transientInstance) {
		log.debug("saving Subject2desc instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Subject2desc persistentInstance) {
		log.debug("deleting Subject2desc instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Subject2desc findById(java.lang.Integer id) {
		log.debug("getting Subject2desc instance with id: " + id);
		try {
			Subject2desc instance = (Subject2desc) getHibernateTemplate().get(
					"com.sundy.service.Subject2desc", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Subject2desc> findByExample(Subject2desc instance) {
		log.debug("finding Subject2desc instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Subject2desc> findByProperty(String propertyName, Object value) {
		log.debug("finding Subject2desc instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Subject2desc as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Subject2desc> findBySubDesc(Integer subjectId, Integer descId) {
		try {
			String queryString = "from Subject2desc as model where model.subjectId=? and model.descId=?";
			return getHibernateTemplate().find(queryString, subjectId, descId);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Subject2desc> findBySubjectId(Object subjectId) {
		return findByProperty(SUBJECT_ID, subjectId);
	}

	public List<Subject2desc> findByDescId(Object descId) {
		return findByProperty(DESC_ID, descId);
	}

	@SuppressWarnings("unchecked")
	public List<Subject2desc> findAll() {
		log.debug("finding all Subject2desc instances");
		try {
			String queryString = "from Subject2desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Subject2desc merge(Subject2desc detachedInstance) {
		log.debug("merging Subject2desc instance");
		try {
			Subject2desc result = (Subject2desc) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Subject2desc instance) {
		log.debug("attaching dirty Subject2desc instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Subject2desc instance) {
		log.debug("attaching clean Subject2desc instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static Subject2descDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (Subject2descDAO) ctx.getBean("Subject2descDAO");
	}
}